package com.joeyzh.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;

import com.joeyzh.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuHuanLin on 2019/1/16.
 *
 * @author by xuHuanLin
 * @Description ExpandableListView公用Adapter
 */

public class BaseExpandableListViewAdapter<T> extends BaseExpandableListAdapter {


    protected Context mContext;
    /**
     * GroupView
     */
    protected View mGroupView;
    /**
     * 组列表
     */
    protected List<T> mGroup = new ArrayList<>();
    /**
     * 展开项
     */
    List<Integer> groupExpandedList = new ArrayList<>();
    /**
     * 当前点击位置
     */
    private int position = -1;

    public BaseExpandableListViewAdapter(Context mContext, List<T> group) {
        this.mContext = mContext;
        this.mGroup = group;
    }

    /**
     * 获得父项的数量
     *
     * @return
     */
    @Override
    public int getGroupCount() {
        if (mGroup == null) {
            return 0;
        }
        return mGroup.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {

        return 0;
    }

    /**
     * 获得某个父项
     *
     * @param groupPosition
     * @return
     */
    @Override
    public T getGroup(int groupPosition) {
        if (groupPosition >= getGroupCount()) {
            return null;
        }
        return mGroup.get(groupPosition);
    }

    @Override
    public Object getChild(int i, int i1) {
        return null;
    }


    /**
     * 获得某个父项的某个子项的id
     *
     * @param parentPos
     * @return
     */
    @Override
    public long getGroupId(int parentPos) {
        return parentPos;
    }

    /**
     * 获得某个父项的某个子项的id
     *
     * @param parentPos
     * @param childPos
     * @return
     */
    @Override
    public long getChildId(int parentPos, int childPos) {
        return childPos;
    }

    /**
     * 按函数的名字来理解应该是是否具有稳定的id，这个方法目前一直都是返回false，没有去改动过
     *
     * @return
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * 子项是否可选中，如果需要设置子项的点击事件，需要返回true
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    /**
     * 设置点击位置
     *
     * @param post
     * @param groupExpandedList
     */
    public void setGroupClickPosition(int post, List<Integer> groupExpandedList) {
        this.position = post;
        this.groupExpandedList = groupExpandedList;
    }


    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
        View view = convertView;
        GroupViewHolder gViewHolder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.ifr_item_expandable_group, null);
            gViewHolder = new GroupViewHolder(view);
            view.setTag(gViewHolder);
        } else {
            gViewHolder = (GroupViewHolder) view.getTag();
        }

        doExpandAnimation(groupPosition, gViewHolder, isExpanded);
//        //当前项点击
//        ImageView imgGpIndicator = gViewHolder.imgGpIndicator;
//        if (position == groupPosition) {
//            Animation rotate;
//            if (isExpanded) {
//                rotate = AnimationUtils.loadAnimation(mContext, com.joey.ui.R.anim.rotate_180);
//            } else {
//                rotate = AnimationUtils.loadAnimation(mContext, com.joey.ui.R.anim.rotate_ccw_180);
//            }
//            rotate.setFillAfter(true);
//
//            imgGpIndicator.setAnimation(rotate);
//        } else {
//            //展开未点击
//            if (position != groupPosition && groupExpandedList.contains(groupPosition) && isExpanded) {
//                //恢复状态
//                imgGpIndicator.setRotation(180);
//            }
//        }
        mGroupView = view;
        return view;
    }

    private void doExpandAnimation(int groupPosition, GroupViewHolder gViewHolder, boolean isExpanded) {
        if (isExpanded) {
            gViewHolder.imgGpIndicator.setRotation(180);
        } else {
            gViewHolder.imgGpIndicator.setRotation(0);
        }
    }

    private void startAnimation(View view, int animRes) {
        Animation anim = AnimationUtils.loadAnimation(mContext, animRes);
        anim.setFillAfter(true);
        view.setAnimation(anim);
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
        return null;
    }

    private class GroupViewHolder {
        ImageView imgGpIndicator;
        View root;

        public GroupViewHolder(View root) {
            this.root = root;
            imgGpIndicator = root.findViewById(R.id.img_gp_indicator);
        }

    }

}
